package cn.xuchunh.campusbbs;

import android.app.Application;

import com.facebook.drawee.backends.pipeline.Fresco;

import java.util.ArrayList;
import java.util.List;

import cn.xuchunh.campusbbs.base.BaseActivity;
import cn.xuchunh.campusbbs.bean.User;
import cn.xuchunh.campusbbs.crash.CrashHandler;
import cn.xuchunh.campusbbs.net.HttpRequestManager;
import cn.xuchunh.campusbbs.support.acache.ACache;

/**
 * Created by XuChunH on 2015/8/31.
 */
public class AppContext extends Application {

    private static AppContext instance;
    private List<BaseActivity> activityList;

    protected User user;
    private ACache cache;

    private int onlineUserNum = 0;
    private int visitorNum = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        user = new User();
        activityList = new ArrayList<>();
        cache = ACache.get(instance);
//        HttpRequestManager.getInstance(instance);
        Fresco.initialize(instance);
        CrashHandler.getInstance().init(getApplicationContext(), false);
    }

    public static AppContext getInstance() {
        return instance;
    }

    public void pushActivity(BaseActivity activity) {
        activityList.add(activity);
    }

    public void popActivity(BaseActivity activity) {
        activityList.remove(activity);
    }

    public void finishActivties() {
        for (BaseActivity baseActivity : activityList) {
            baseActivity.finish();
        }
    }

    public User getUser() {
        if (user == null) {
            user = (User) cache.getAsObject("user");
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
        cache.put("user", user);
    }

    public ACache getCache() {
        return cache;
    }

    public int getOnlineUserNum() {
        return onlineUserNum;
    }

    public void setOnlineUserNum(int onlineUserNum) {
        this.onlineUserNum = onlineUserNum;
    }

    public int getVisitorNum() {
        return visitorNum;
    }

    public void setVisitorNum(int visitorNum) {
        this.visitorNum = visitorNum;
    }

    public void exit() {
        HttpRequestManager.getInstance(instance).stop();
    }

}
